﻿using UnityEngine;
using System.Collections;

public class Hider : MonoBehaviour
{
    private bool touching = false;
    private float sensivity = 0.1f;
    private Vector2 touchBegin = new Vector2(0, 0);
    private Vector2 direction = new Vector2(0, 0);

    private float xRotation = 0;
    private float yRotation = 0;
    private float zRotation = 0;

    private float zPosition = 0;


    // Use this for initialization
    void Start()
    {
        zPosition = transform.position.z;

        xRotation = transform.localEulerAngles.x;
        yRotation = transform.localEulerAngles.y;
        zRotation = transform.localEulerAngles.z;

        Vector3 transformTemp = transform.position;

        transformTemp.x = DataStorage.Instance.GetRoomList()[0].GetCenter().x;
        transformTemp.y = DataStorage.Instance.GetRoomList()[0].GetCenter().y;

        transform.position = transformTemp;

    }

    // Update is called once per frame
    void Update()
    {
        Move();
        transform.position = new Vector3(transform.position.x, transform.position.y, zPosition);
    }

    private void Move()
    {
        if (GameState.Instance.GetState().Equals(State.HidingPhase))
        {
#if UNITY_ANDROID && !UNITY_EDITOR
        if (touching)
            this.GetComponent<CharacterController>().Move(new Vector3(direction.x, direction.y, 0) * sensivity);

        if (Input.touchCount > 0)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                if (!(Input.touches[0].position.x < Screen.width / 2 && Input.touches[0].position.y < Screen.height / 4))
                {
                    Debug.Log("Touch began at: " + Input.touches[0].position.x + " " + Input.touches[0].position.y);
                    touchBegin = Input.touches[0].position;
                    touching = true;
                }
            }
            if (Input.touches[0].phase == TouchPhase.Moved)
            {
                direction = Input.touches[0].position - touchBegin;
                float vectorLength = Mathf.Sqrt(direction.x * direction.x + direction.y * direction.y);
                direction = direction / vectorLength;
            }
            if (Input.touches[0].phase == TouchPhase.Ended)
                touching = false;
        }
#else
            this.GetComponent<CharacterController>().Move(new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0));
#endif

            //maxScreenY    
            //
            //
            //
            //
            //
            /*************************/
            // 0,0                    maxScreenX  
        }
        else if (GameState.Instance.GetState().Equals(State.SeekingPhase))
            GetComponent<Hider>().enabled = false;

    }

    public void Rotate(float sliderValue)
    {
        Debug.Log("x: " + transform.rotation.x);
        Debug.Log("z: " + transform.rotation.z);

        Vector3 temp = transform.localEulerAngles;

        temp.z = sliderValue;

        //transform.eulerAngles = new Vector3(xRotation, 360*sliderValue, zRotation );
        transform.localEulerAngles = temp;//new Vector3(sliderValue, yRotation , zRotation);
        //transform.rotation = Quaternion.AngleAxis(sliderValue, Vector3.right);
        //transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, sliderValue); // this is 90 degrees around y axis
    }
}
